#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000100;
int tree[maxn][3];
int fa[maxn];
int dis[maxn];
int tol;
int tot;
int head[maxn];
struct node
{
	int to;
	int val;
	int next;
}edge[maxn];
void addedge(int u,int v,int w)
{
	edge[tol].to = v;
	edge[tol].val = w;
	edge[tol].next = head[u];
//	printf("%d\n",edge[tol].next);
	head[u] = tol++;
//	printf("head[%d] =%d\n",u,head[u]);
}
void dfs(int pi)
{
	queue<int>qe;
	dis[pi] = 0;
	fa[pi] = pi;
	qe.push(pi);
	while(!qe.empty())
	{
		int u = qe.front();
		qe.pop();
		//printf("-- %d\n",head[u]);
		
		for(int i = head[u];i!=-1;i = edge[i].next)
		{
		//	printf("- %d\n",u);
			int v = edge[i].to;
		//	printf("* %d\n",v);
		//	printf("+++++%d\n",edge[i].next);
			if(v==fa[u]) continue;
		//	printf("+++++%d\n",dis[v]);
			dis[v] = dis[u]^edge[i].val;
			fa[v] = u;
			qe.push(v);
		}
	}
}
void insert(int dis)
{
	int rt = 0;
	for(int i = 30;i>0;i--)
	{
		int id = dis>>i&1;
		if(!tree[rt][id])
		{
			tree[rt][id] = ++tot;
		}
		rt = tree[rt][id];
	}
}
int find(int dis)
{
	int rt = 0;
	int ans = 0;
//	printf("+++++%d\n",dis);
	for(int i =4;i>=0;i--)
	{
		int id = (dis>>i)&1;
		ans=ans<<1;
	//	printf("%d\n",id);
		if(tree[rt][!id])
		{
		//	printf("+++++++\n");
			rt = tree[rt][!id];
			ans++; 
		}
		else rt = tree[rt][id];
		
	}
	//printf("***%d\n",ans);
	return ans;
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		tol = 0;
		memset(head,-1,sizeof(head));
		memset(tree,0,sizeof(tree));
		for(int i = 1;i<=n-1;i++)
		{
			int u,v,w;
			scanf("%d %d %d",&u,&v,&w);
			addedge(u,v,w);
			addedge(v,u,w);
		}
	//	for(int i = 0;i<n;i++) printf("......%d\n",head[i]);
		dfs(0);
		tot = 0;
		for(int i = 0;i<n;i++)
		{
			insert(dis[i]);
		}
		int ans = 0;
		for(int i = 0;i<n;i++)
		{
		//	printf("****%d\n",dis[i]);
			ans = max(ans,find(dis[i]));
		}
		printf("%d\n",ans);
	}
}
